package com.sinven.service;

import com.sinven.mapper.UserMapper;
import com.sinven.pojo.User;
import com.sinven.util.EncryptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    @Transactional
    public User login(String userCode, String userPwd) throws Exception {
        User user = userMapper.queryByUserCode(userCode);
        if (user == null || user.getUserCode().isEmpty()){
            throw new Exception("用户不存在");
        }
        if(!user.getUserPwd().equals(EncryptionUtil.getSha1(userPwd))){
            throw new Exception("密码错误");
        }
        //更新登录时间
        userMapper.updateLastLoginTime(user.getUserId());
        return user;
    }
}
